//
//  SchoolDetailContentTableViewCell.swift
//  HoneyBee
//
//  Created by point on 2018/6/2.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit



protocol SchoolDetailContentTableViewCellDelegate : class {
    func heightReload()
    
}

class SchoolDetailContentTableViewCell: UITableViewCell {
    
    var currentModel:SchoolDetailModel!
    weak var delegate : SchoolDetailContentTableViewCellDelegate?
    
    fileprivate var timer:Timer!
    fileprivate var lastHeight:CGFloat = 0.0
    
    
    
    lazy var editorView: RichEditorView = {
        let editorView = RichEditorView()
        editorView.isEditingEnabled = false
        editorView.isScrollEnabled = false
        editorView.delegate = self
        
        return editorView
    }()
    
   override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        //addTimer()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        //removeTimer()
        //debugPrint("deinit: htmlcell")
    }
}


extension SchoolDetailContentTableViewCell: RichEditorDelegate {
    func richEditor(_ editor: RichEditorView, heightDidChange height: Int) {
        self.contentView.layoutIfNeeded()
        currentModel.detailCellHeight =  CGFloat(height)
        editorView.height = CGFloat(height)
        delegate?.heightReload()
    }
    
}

// MARK: - 加载高度优化
extension SchoolDetailContentTableViewCell {
    fileprivate func addTimer() {
        timer = Timer(timeInterval: 1.5, target: self, selector: #selector(nextpage), userInfo: nil, repeats: true)
        RunLoop.main.add(timer, forMode:.common)
    }
    
    fileprivate func removeTimer() {
        timer.invalidate()
        timer = nil
    }
    
    @objc fileprivate func nextpage() {
        removeTimer()
        delegate?.heightReload()
    }
}

// MARK:- 显示数据
extension SchoolDetailContentTableViewCell {
    func configModel(model:SchoolDetailModel) {
        //let str = "<p><p><span style=\"text-decoration: underline;\">你说你是谁 你是放马的猴</span></p ><p><span style=\"color: rgb(194, 79, 74);\">哎呀</span></p ><p><span style=\"color: rgb(194, 79, 74);\"><br></span></p ><p><span style=\"color: rgb(194, 79, 74);\">怪该</span></p >"
        currentModel = model
        let info = "<html><head><meta charset=\"UTF-8\"><style type=\"text/css\">img {max-width:\(kScreenW - 20)px;}</style></head><body>\(model.info)</body></html>"
        editorView.backgroundColor = UIColor.white
        editorView.html = info
    }
}

extension SchoolDetailContentTableViewCell {
    fileprivate func setupUI(){
        contentView.addSubview(editorView)
        editorView.backgroundColor = UIColor.white
        editorView.frame  = CGRect(x: 10, y: 0, width: kScreenW - 20, height: 100)
//        editorView.snp.makeConstraints { (make) in
//            make.edges.equalTo(contentView)
//        }
    }
}
